package com.chlitina.store.modules.cash.model.recharge;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.chlitina.store.modules.base.entity.StoreCardTemplateAll;
import com.chlitina.store.modules.base.entity.StoreCardTemplateDuration;
import com.chlitina.store.modules.base.entity.StoreCardTemplateProduct;
import com.chlitina.store.modules.base.entity.StoreCardTemplateProductDetail;
import com.chlitina.store.modules.base.entity.StoreCardTemplateTimes;
import com.chlitina.store.modules.base.entity.StoreCare;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.cash.entity.StoreCardAll;
import com.chlitina.store.modules.cash.entity.StoreCardDuration;
import com.chlitina.store.modules.cash.entity.StoreCardProduct;
import com.chlitina.store.modules.cash.entity.StoreCardTimes;
import com.chlitina.store.modules.cash.model.AllCardPayment;
import com.chlitina.store.modules.cash.model.DurationCardPayment;
import com.chlitina.store.modules.cash.model.ProductCardPayment;
import com.chlitina.store.modules.cash.model.TimesCardPayment;

public class CardDetailFactory {
	public static CardDetail createCardDetail(Object card) {
		if (card instanceof ProductCardPayment) {
			return createCardDetail(((ProductCardPayment)card).getCard());
		} else if (card instanceof DurationCardPayment) {
			return createCardDetail(((DurationCardPayment)card).getCard());

		} else if (card instanceof TimesCardPayment) {
			return createCardDetail(((TimesCardPayment)card).getCard());

		} else if (card instanceof AllCardPayment) {
			return createCardDetail(((AllCardPayment)card).getCard());
		}
		return null;
	}

	public static CardDetail createCardDetail(StoreCardAll card) {
		CardDetailAll detailAll = new CardDetailAll();
		StoreCardTemplateAll template = card.getTemplate();
		detailAll.setName(template.getName());
		detailAll.setPrice(template.getReal_cost());
		detailAll.setRetailPrice(template.getRetail_price());
		detailAll.setBeginDate(card.getBeginTime());
		detailAll.setCareDiscount(card.getCareDiscount());
		detailAll.setProductDiscount(card.getProductDiscount());
		detailAll.setRemainMoney(card.getMoney());
		detailAll.setState(card.getStatus() == 0 ? "有效" : "无效");
		return detailAll;
	}

	static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"yyyy年MM月dd日 HH时mm分ss秒");

	public static void main(String[] args) {
		System.out.println(simpleDateFormat.format(new Date()));
	}

	public static CardDetail createCardDetail(StoreCardTimes card) {
		CardDetailTimes times = new CardDetailTimes();
		times.setBeginDate(card.getBeginTime());
		times.setState(card.getStatus() == 0 ? "有效" : "无效");
		StoreCardTemplateTimes template = card.getTemplate();
		StoreCare storeCare = template.getStoreCare();
		times.setName(template.getName());
		if (storeCare.getCare_type() == StoreCare.TYPE_SELF) {
			times.setCareName(storeCare.getStoreSelfCare().getName());
			times.setCareDuration(storeCare.getStoreSelfCare().getDuration());
		} else {
			times.setCareName(storeCare.getCare().getName());
			times.setCareDuration(storeCare.getCare().getDuration());
		}
		times.setTimes(template.getTimes());
		times.setRemainTimes(card.getRemainTimes());
		times.setPrice(template.getReal_cost());
		times.setRetailPrice(template.getRetail_price());
		return times;
	}

	public static CardDetail createCardDetail(StoreCardDuration card) {
		CardDetailDuration detailDuration = new CardDetailDuration();
		Date beginTime = card.getBeginTime();
		detailDuration.setBeginDate(beginTime);
		Date endTime = card.getEndTime();
		detailDuration.setState(card.getStatus() == 0 ? "有效" : "无效");
		 StoreCardTemplateDuration template = card.getTemplate();
		StoreCare storeCare = template.getStoreCare();
		detailDuration.setName(template.getName());
		if (storeCare.getCare_type() == StoreCare.TYPE_SELF) {
			detailDuration.setCareName(storeCare.getStoreSelfCare().getName());
			detailDuration.setCareDuration(storeCare.getStoreSelfCare().getDuration());
		} else {
			detailDuration.setCareName(storeCare.getCare().getName());
			detailDuration.setCareDuration(storeCare.getCare().getDuration());
		}
		int offsetYear=endTime.getYear()-beginTime.getYear();
		int offsetMounth=offsetYear*12+(endTime.getMonth()-beginTime.getMonth());
		detailDuration.setDuration(offsetMounth);
		detailDuration.setEndDate(endTime);
		detailDuration.setRechargeDuration(template.getDuration());
		detailDuration.setPrice(template.getReal_cost());
		detailDuration.setRetailPrice(template.getRetail_price());
		return detailDuration;
	}

	private static CardDetail createCardDetail(StoreCardProduct card) {
		CardDetailProduct detailProduct = new CardDetailProduct();
		detailProduct.setBeginDate(card.getBeginTime());
		detailProduct.setState(card.getStatus() == 0 ? "有效" : "无效");
		StoreCardTemplateProduct template = card.getTemplate();
		detailProduct.setName(template.getName());
		detailProduct.setProductDiscount(template.getDiscount());
		detailProduct.setRemainMoney(card.getMoney());
		detailProduct.setRetailPrice(template.getRetail_price());
		detailProduct.setPrice(template.getReal_cost());
		List<StoreCardTemplateProductDetail> productList = template
				.getProductList();
		List<String> products = detailProduct.getProducts();
		StoreProduct storeProduct = null;
		for (StoreCardTemplateProductDetail pDetail : productList) {
			storeProduct = pDetail.getStoreProduct();
			if (storeProduct.getProduct_type() == StoreProduct.TYPE_SELF) {
				products.add(storeProduct.getStoreSelfProduct().getName());
			} else {
				products.add(storeProduct.getProduct().getName());
			}
		}
		return detailProduct;
	}
}
